package com.vancouver.knows.ui.activity.my;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.Bind;
import butterknife.ButterKnife;

import com.pangu.AMApplication;
import com.pangu.bean.PageInfo;
import com.pangu.service.ActionException;
import com.pangu.task.mark.ATaskMark;
import com.pangu.ui.browser.PullListItemBrowser;
import com.pangu.ui.refresh.ARefreshViewController;
import com.vancouver.knows.MainApplication;
import com.vancouver.knows.R;
import com.vancouver.knows.ui.activity.citynews.CityNewsDetailActivity;
import com.vancouver.knows.business.BSharedPrefManager;
import com.vancouver.knows.business.BusinessModule;
import com.vancouver.knows.business.bean.MyArticleInfo;
import com.vancouver.knows.business.service.impl.MyArticleHandle;
import com.vancouver.knows.business.task.mark.MyArticleTaskMark;

@SuppressWarnings("rawtypes")
public class MyArticleListView extends PullListItemBrowser<AMApplication> {
	private Context mContext;
	private BusinessModule bModule;

	public MyArticleListView(Context context, ARefreshViewController mController) {
		super(context, mController);
		mContext = context;
		bModule = ((MainApplication) imContext).getBusinessModule();
	}

	@SuppressWarnings("deprecation")
	@Override
	protected void setListViewParameters(ListView listView) {
		super.setListViewParameters(listView);
		listView.setDivider(new ColorDrawable(getResources().getColor(R.color.line_color)));
		listView.setDividerHeight(1);
	}

	@Override
	public void receiveResult(ATaskMark taskMark, ActionException exception, Object trackerResult) {
		super.receiveResult(taskMark, exception, trackerResult);
		if (exception != null && !exception.getExMessage().equals("")) {
			Toast.makeText(mContext, exception.getExMessage(), Toast.LENGTH_LONG).show();
			return;
		}
		if (taskMark instanceof MyArticleTaskMark && taskMark.getTaskStatus() == ATaskMark.HANDLE_OVER)
			notifyDataSetChanged();
	}

	@Override
	protected void addListHeader(ListView listView) {
		super.addListHeader(listView);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		MyArticleInfo myArticleInfo = (MyArticleInfo) parent.getAdapter().getItem(position);
		Intent intent = new Intent(getContext(), CityNewsDetailActivity.class);
		intent.putExtra("postId", myArticleInfo.getId());
		mContext.startActivity(intent);
		haveNewArticleCommentHide(myArticleInfo);
		notifyDataSetChanged();
	}

	private void haveNewArticleCommentHide(MyArticleInfo myArticleInfo) {
		if (myArticleInfo != null) {
			if (myArticleInfo.getNewCommentCount() > 0) {
				myArticleInfo.setNewCommentCount(0);
			}
		}

	}

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
		return false;
	}

	@Override
	protected BaseAdapter createAdapter() {
		return new MyArticleAdapter();
	}

	@Override
	protected void handleLoadNewItems(ATaskMark taskMark) {
		// 发送登录需要的参数
		MyArticleTaskMark myArticleTaskMark = (MyArticleTaskMark) taskMark;
		PageInfo pageInfo = myArticleTaskMark.getPageInfo();
		BSharedPrefManager sharedPrefManager = ((MainApplication) imContext).getSharedPrefManager();
		bModule.getServiceWrapper().myArticleFollowAndComments(this, myArticleTaskMark, sharedPrefManager.getUserId(),
				String.valueOf(pageInfo.getNextFrom()), String.valueOf(pageInfo.getSize()), null,
				MyArticleHandle.USER_POSTED_ARTICLES);
	}

	public class MyArticleAdapter extends BaseAdapter {
		private LayoutInflater lf;

		public MyArticleAdapter() {
			this.lf = LayoutInflater.from(getContext());
		}

		@Override
		public int getCount() {
			return bModule.getCacheManager().getMyArticleCache().getItemInfoCount(mTaskMark);
		}

		@Override
		public Object getItem(int position) {
			return bModule.getCacheManager().getMyArticleCache().getItemInfoByTypeIndex(mTaskMark, position);

		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			ViewValueHand hand = null;
			View view = convertView;
			if (position == getCount() - 1) {
				tryQueryNewItems(true, ATaskMark.TYPE_DEFAULT);
			}
			if (convertView == null) {
				view = lf.inflate(R.layout.my_article_item, parent, false);
				hand = new ViewValueHand(view);

				view.setTag(hand);
			} else {
				hand = (ViewValueHand) view.getTag();
			}

			MyArticleInfo info = (MyArticleInfo) getItem(position);
			hand.newCommentText.setVisibility(View.GONE);
			if (info.getNewCommentCount() > 0) {
				hand.newCommentText.setVisibility(View.VISIBLE);
			}

			hand.articleText.setText(info.getPostTitle());
			return view;
		}

		public class ViewValueHand {
			@Bind(R.id.article_text)
			TextView articleText;
			@Bind(R.id.new_comment)
			TextView newCommentText;

			public ViewValueHand(View view) {
				ButterKnife.bind(this, view);
			}
		}
	}
}